package com.kristou.urgdroid;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.android.looper.LoopSurfaceView;
import com.kristou.urgdriver.sensor.RangeSensor;

/**
 * Created by Kristou on 13/10/03.
 */
public class PlotFragment extends Fragment {

    private static final String TAG = PlotFragment.class.getSimpleName();

    private View mView;
    private LoopSurfaceView mLoopSurfaceView;
    private MeasurementAdapter mMeasurement = new MeasurementAdapter();
    private Button mPlayButton;
    private Button mStopButton;

    public void play() {
        if (getPlayButton().isEnabled()) {
            getPlayButton().setEnabled(!mMeasurement.start());
            getStopButton().setEnabled(!getPlayButton().isEnabled());
        }
    }

    public void stop() {
        if (getStopButton().isEnabled()) {
            getPlayButton().setEnabled(!mMeasurement.stop());
            getStopButton().setEnabled(!getPlayButton().isEnabled());
        }
    }

    public void setSensor(RangeSensor sensor) {
        mMeasurement.setSensor(sensor);
        getPlayButton().setEnabled(sensor != null && sensor.isConnected());
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_plot, container, false);
        getLoopSurfaceView().setAdapter(mMeasurement);
        getPlayButton().setEnabled(false);
        getStopButton().setEnabled(false);

        getPlayButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                play();
            }
        });

        getStopButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stop();
            }
        });

        mMeasurement.setDistanceColor(getResources().getColor(android.R.color.holo_green_dark));

        return mView;
    }

    @Override
    public void onPause() {
        super.onPause();
        stop();
        getLoopSurfaceView().pause();
    }

    @Override
    public void onResume() {
        super.onResume();
        getLoopSurfaceView().play();
    }

    public LoopSurfaceView getLoopSurfaceView() {
        if (mLoopSurfaceView == null) {
            mLoopSurfaceView = (LoopSurfaceView) mView.findViewById(R.id.surfaceview);
        }
        return mLoopSurfaceView;
    }

    public Button getPlayButton() {
        if (mPlayButton == null) {
            mPlayButton = (Button) mView.findViewById(R.id.btnPlay);
        }
        return mPlayButton;
    }

    public Button getStopButton() {
        if (mStopButton == null) {
            mStopButton = (Button) mView.findViewById(R.id.btnStop);
        }
        return mStopButton;
    }
}
